package com.music.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.HashMap;


public class QiniuCloudUtil {
    private static final Logger logger= LoggerFactory.getLogger(QiniuCloudUtil.class);
    //设置好账号的ACCESS_KEY和SECRET_KEY
    private static String ACCESS_KEY = "2uMzxhd4oklT6_ahG7Qan893JiVkqLgQuBAtZ4G3";
    private static String SECRET_KEY = "5qQ3vH7zBKNf1U1zvLaxn-d7ZWE7BK0m-Q6IeuXE";
    //要上传的空间
    private static String bucketname = "lhxzone";


    //密钥配置
    private static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

    private static Configuration c = new Configuration(Region.huanan());

    //创建上传对象
    private static UploadManager uploadManager = new UploadManager(c);
    private static String QINIU_IMAGE_DOMAIN="http://rdvp21b9j.hn-bkt.clouddn.com/";

    public static String getUpToken(){
        return auth.uploadToken(bucketname);
    }
    public static String upload(MultipartFile file ,String storeFileName) throws IOException{
        try {
            Response res=uploadManager.put(file.getBytes(),storeFileName,getUpToken());
            if(res.isOK()){
                logger.info("++++++"+res.bodyString());
                logger.info("++++++++"+QINIU_IMAGE_DOMAIN+new ObjectMapper().readValue(res.bodyString(),HashMap.class).get("key").toString());
                return QINIU_IMAGE_DOMAIN+new ObjectMapper().readValue(res.bodyString(), HashMap.class).get("key").toString();
            }else{
                logger.error("七牛异常："+res.toString());
                return null;
            }

        }catch (QiniuException e){
            Response r=e.response;
            logger.error("七牛异常："+r.toString());
            return null;
        }
    }
    public static String  getDownloadUrl(String fileName){
        String publicUrl=QINIU_IMAGE_DOMAIN+fileName;
        //String downloadUrl=auth.privateDownloadUrl(publicUrl,3600);
        logger.info("+++++++"+publicUrl);
        return publicUrl;
    }
    /**
     * 删除指定文件
     * @param fileName
     * @return
     */
    public static String delete(String fileName){
        //设置需要操作的账号的AK和SK
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

        Configuration c = new Configuration(Region.huanan());

        //实例化一个BucketManager对象
        BucketManager bucketManager = new BucketManager(auth, c);
        //要测试的空间和key，并且这个key在你空间中存在
        try {
            //调用delete方法移动文件
            bucketManager.delete(bucketname, fileName);
        } catch (QiniuException e) {
            //捕获异常信息
            Response r = e.response;
            System.out.println(r.toString());
            return "删除失败";
        }
        return "删除---------->"+fileName+"成功";
    }






}

